/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package srl90a03.impl;

import bRM_LL73.BRM_LL73;
import java.util.Collection;
import node1.AbstractTask;
import node1.impl.Node1Impl;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;
import pUNB.PUNB;
import result.Results;
import srl90a03.Srl90a03;
import srl90a03.Srl90a03Package;
import srl90a03.Task;
import utils.UtilsModelInstance;
import zFactory.NodeLiu73a01;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Srl90a03</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link srl90a03.impl.Srl90a03Impl#getPU <em>PU</em>}</li>
 *   <li>{@link srl90a03.impl.Srl90a03Impl#getBound <em>Bound</em>}</li>
 *   <li>{@link srl90a03.impl.Srl90a03Impl#getTasks <em>Tasks</em>}</li>
 *   <li>{@link srl90a03.impl.Srl90a03Impl#getBlockingTerm <em>Blocking Term</em>}</li>
 *   <li>{@link srl90a03.impl.Srl90a03Impl#getValue <em>Value</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class Srl90a03Impl extends Node1Impl implements Srl90a03 {
	/**
	 * The cached value of the '{@link #getPU() <em>PU</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPU()
	 * @generated
	 * @ordered
	 */
	protected PUNB pU;

	/**
	 * The cached value of the '{@link #getBound() <em>Bound</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBound()
	 * @generated
	 * @ordered
	 */
	protected BRM_LL73 bound;

	/**
	 * The cached value of the '{@link #getTasks() <em>Tasks</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTasks()
	 * @generated
	 * @ordered
	 */
	protected EList<Task> tasks;

	/**
	 * The default value of the '{@link #getBlockingTerm() <em>Blocking Term</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBlockingTerm()
	 * @generated
	 * @ordered
	 */
	protected static final double BLOCKING_TERM_EDEFAULT = 0.0;

	/**
	 * The cached value of the '{@link #getBlockingTerm() <em>Blocking Term</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBlockingTerm()
	 * @generated
	 * @ordered
	 */
	protected double blockingTerm = BLOCKING_TERM_EDEFAULT;

	/**
	 * The default value of the '{@link #getValue() <em>Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getValue()
	 * @generated
	 * @ordered
	 */
	protected static final double VALUE_EDEFAULT = 0.0;

	/**
	 * The cached value of the '{@link #getValue() <em>Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getValue()
	 * @generated
	 * @ordered
	 */
	protected double value = VALUE_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected Srl90a03Impl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return Srl90a03Package.Literals.SRL90A03;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PUNB getPU() {
		if (pU != null && pU.eIsProxy()) {
			InternalEObject oldPU = (InternalEObject)pU;
			pU = (PUNB)eResolveProxy(oldPU);
			if (pU != oldPU) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, Srl90a03Package.SRL90A03__PU, oldPU, pU));
			}
		}
		return pU;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PUNB basicGetPU() {
		return pU;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPU(PUNB newPU) {
		PUNB oldPU = pU;
		pU = newPU;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, Srl90a03Package.SRL90A03__PU, oldPU, pU));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public BRM_LL73 getBound() {
		if (bound != null && bound.eIsProxy()) {
			InternalEObject oldBound = (InternalEObject)bound;
			bound = (BRM_LL73)eResolveProxy(oldBound);
			if (bound != oldBound) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, Srl90a03Package.SRL90A03__BOUND, oldBound, bound));
			}
		}
		return bound;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public BRM_LL73 basicGetBound() {
		return bound;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBound(BRM_LL73 newBound) {
		BRM_LL73 oldBound = bound;
		bound = newBound;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, Srl90a03Package.SRL90A03__BOUND, oldBound, bound));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Task> getTasks() {
		if (tasks == null) {
			tasks = new EObjectContainmentEList<Task>(Task.class, this, Srl90a03Package.SRL90A03__TASKS);
		}
		return tasks;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public double getBlockingTerm() {
		return blockingTerm;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBlockingTerm(double newBlockingTerm) {
		double oldBlockingTerm = blockingTerm;
		blockingTerm = newBlockingTerm;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, Srl90a03Package.SRL90A03__BLOCKING_TERM, oldBlockingTerm, blockingTerm));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public double getValue() {
		return value;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setValue(double newValue) {
		double oldValue = value;
		value = newValue;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, Srl90a03Package.SRL90A03__VALUE, oldValue, value));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case Srl90a03Package.SRL90A03__TASKS:
				return ((InternalEList<?>)getTasks()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case Srl90a03Package.SRL90A03__PU:
				if (resolve) return getPU();
				return basicGetPU();
			case Srl90a03Package.SRL90A03__BOUND:
				if (resolve) return getBound();
				return basicGetBound();
			case Srl90a03Package.SRL90A03__TASKS:
				return getTasks();
			case Srl90a03Package.SRL90A03__BLOCKING_TERM:
				return new Double(getBlockingTerm());
			case Srl90a03Package.SRL90A03__VALUE:
				return new Double(getValue());
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case Srl90a03Package.SRL90A03__PU:
				setPU((PUNB)newValue);
				return;
			case Srl90a03Package.SRL90A03__BOUND:
				setBound((BRM_LL73)newValue);
				return;
			case Srl90a03Package.SRL90A03__TASKS:
				getTasks().clear();
				getTasks().addAll((Collection<? extends Task>)newValue);
				return;
			case Srl90a03Package.SRL90A03__BLOCKING_TERM:
				setBlockingTerm(((Double)newValue).doubleValue());
				return;
			case Srl90a03Package.SRL90A03__VALUE:
				setValue(((Double)newValue).doubleValue());
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case Srl90a03Package.SRL90A03__PU:
				setPU((PUNB)null);
				return;
			case Srl90a03Package.SRL90A03__BOUND:
				setBound((BRM_LL73)null);
				return;
			case Srl90a03Package.SRL90A03__TASKS:
				getTasks().clear();
				return;
			case Srl90a03Package.SRL90A03__BLOCKING_TERM:
				setBlockingTerm(BLOCKING_TERM_EDEFAULT);
				return;
			case Srl90a03Package.SRL90A03__VALUE:
				setValue(VALUE_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case Srl90a03Package.SRL90A03__PU:
				return pU != null;
			case Srl90a03Package.SRL90A03__BOUND:
				return bound != null;
			case Srl90a03Package.SRL90A03__TASKS:
				return tasks != null && !tasks.isEmpty();
			case Srl90a03Package.SRL90A03__BLOCKING_TERM:
				return blockingTerm != BLOCKING_TERM_EDEFAULT;
			case Srl90a03Package.SRL90A03__VALUE:
				return value != VALUE_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (blockingTerm: ");
		result.append(blockingTerm);
		result.append(", value: ");
		result.append(value);
		result.append(')');
		return result.toString();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void compute() {

		/**/
		EList<AbstractTask> list = new BasicEList<AbstractTask>();
		for (srl90a03.Task t : this.tasks) {
			list.add(t);
		}
		
		if (!done  && this.tasks!=null && this.pU!=null)
		{
			if (this.bound == null) UtilsModelInstance.setNode(this, new NodeLiu73a01(this.tasks.size(), true));
			
			/* Calculate the blocking term*/
			for (srl90a03.Task task : this.tasks)
			{
				double blocking = (1.0*task.getBlocking())/(1.0*task.getPeriod());
				if (blocking > this.blockingTerm) this.blockingTerm = blocking; 
			}

			this.value = this.pU.getU() + this.blockingTerm;

			if (this.bound.getValue() >= this.value) 
			{
				UtilsModelInstance.createSchedulingResult(this, Results.SCHEDULABLE);
			}
			else {
				UtilsModelInstance.createSchedulingResult(this, Results.UNDETERMINED);
			}
			this.done = true;
		}

	} // compute()
} //Srl90a03Impl
